package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.goods.feign.SkuFeign;
import com.itheima.goods.pojo.Sku;
import com.itheima.order.feign.OrderFeign;
import com.itheima.order.feign.OrderItemFeign;
import com.itheima.order.pojo.Order;
import com.itheima.order.pojo.OrderItem;
import com.itheima.user.feign.ItemsCollectFeign;
import com.itheima.user.feign.ItemsHistoryFeign;
import com.itheima.user.pojo.ItemsCollect;
import com.itheima.user.pojo.ItemsHistory;
import com.itheima.util.IdWorker;
import com.itheima.util.Result;
import com.itheima.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/center")
@CrossOrigin
public class ReceiveController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private OrderItemFeign orderItemFeign;

    @Autowired
    private ItemsHistoryFeign itemsHistoryFeign;

    @Autowired
    private ItemsCollectFeign itemsCollectFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private IdWorker idWorker;

    /**
     * 待发货待收货
     *
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String receiveList(Model model) {
        // 查询order待发货的数据
        Order order = new Order();
        order.setUsername("itheima");
        order.setPayStatus("1");
        order.setConsignStatus("0");
        order.setIsDelete("0");
        Result<List<Order>> listResult = orderFeign.findList(order);
        List<Order> result = listResult.getData();
        // 查询orderitem待发货的数据
        OrderItem orderItem = new OrderItem();
        for (Order ordersb : result) {
            orderItem.setOrderId(ordersb.getId());
            Result<List<OrderItem>> list = orderItemFeign.findList(orderItem);
            List<OrderItem> orderItems = list.getData();
            ordersb.setOrderItems(orderItems);
        }
        model.addAttribute("result", result);
        // 查询order待收货的数据
        order.setConsignStatus("1");
        Result<List<Order>> orderFeignList = orderFeign.findList(order);
        List<Order> orders = orderFeignList.getData();
        // 查询orderitem待收货的数据
        for (Order ordersbs : orders) {
            orderItem.setOrderId(ordersbs.getId());
            Result<List<OrderItem>> list = orderItemFeign.findList(orderItem);
            List<OrderItem> orderItems = list.getData();
            ordersbs.setOrderItems(orderItems);
        }
        // 设置传前端的值
        String url = "/center/receive";
        model.addAttribute("orders", orders);
        model.addAttribute("url", url);
        return "myOrder";
    }

    /**
     * 确认收货
     *
     * @param model
     * @param id    前端传的订单id
     * @return
     */
    @RequestMapping("/receive")
    public String confirmReceive(Model model, String id) {
        Order order = orderFeign.findById(id).getData();
        order.setOrderStatus("1");
        order.setConsignStatus("2");
        orderFeign.update(order, id);
        return receiveList(model);
    }

    /**
     * 我的足迹商品展示
     *
     * @param model
     * @return
     */
    @RequestMapping("/footmark")
    public String footmark(Model model) {
        // 浏览足迹的列表
        ItemsHistory itemsHistory = new ItemsHistory();
        itemsHistory.setUsername("itheima");
        List<ItemsHistory> itemsHistoryList = itemsHistoryFeign.findList(itemsHistory).getData();
        for (ItemsHistory history : itemsHistoryList) {
            String id = history.getSkuId();
            Sku sku = skuFeign.findById(id).getData();
            history.setSku(sku);
        }
        model.addAttribute("itemsHistoryList", itemsHistoryList);
        return "seckillperson-footmark";
    }

    /**
     * 查询订单中心的商品, 并将skuid存入itemshistory中, 返回商品的详情页
     * @param model
     * @param id    前端传来的skuId
     * @return
     */
    @RequestMapping("/view")
    public String view(Model model, String id) {
        // 根据itemsHistory的条件进行查询
        ItemsHistory itemsHistory = new ItemsHistory();
        itemsHistory.setUsername("itheima");
        itemsHistory.setSkuId(id);
        List<ItemsHistory> histories = itemsHistoryFeign.findList(itemsHistory).getData();
        // 判断是不是重复浏览商品
        if (histories != null && histories.size() > 0) {
            // 是就改时间
            for (ItemsHistory history : histories) {
                history.setViewTime(new Date());
                itemsHistoryFeign.update(history, history.getId());
                history.setSku(skuFeign.findById(id).getData());
                // 返回商品详情页
                return "http://item-itheima-java.itheima.net/" + history.getSku().getSpuId() + ".html";
            }
        }
        // 不是就在数据库增加浏览数据
        itemsHistory.setViewTime(new Date());
        itemsHistory.setId("No" + idWorker.nextId());
        itemsHistoryFeign.add(itemsHistory);
        itemsHistory.setSku(skuFeign.findById(id).getData());
        // 返回商品详情页
        return "http://item-itheima-java.itheima.net/" + itemsHistory.getSku().getSpuId() + ".html";
    }

    /**
     * 商品收藏的商品展示
     *
     * @param model
     * @return
     */
    @RequestMapping("/collect")
    public String collect(Model model) {
        // 浏览足迹的列表
        ItemsCollect itemsCollect = new ItemsCollect();
        itemsCollect.setUserid("itheima");
        List<ItemsCollect> itemsCollectList = itemsCollectFeign.findList(itemsCollect).getData();
        for (ItemsCollect collect : itemsCollectList) {
            String id = collect.getSkuid();
            Sku sku = skuFeign.findById(id).getData();
            collect.setSku(sku);
        }
        model.addAttribute("itemsCollectList", itemsCollectList);
        return "seckillperson-collect";
    }

    /**
     * 在商品详情页点击商品收藏后, 将收藏商品添加到数据库, 返回商品详情页
     * @param model
     * @param id 前端传来的skuId
     * @return
     */
    @RequestMapping("/gather")
    @ResponseBody
    public Result gather(Model model, @RequestParam String id) {
        // 根据itemsHistory的条件进行查询
        ItemsCollect itemsCollect = new ItemsCollect();
        itemsCollect.setUserid("itheima");
        itemsCollect.setSkuid(id);
        List<ItemsCollect> collectList = itemsCollectFeign.findList(itemsCollect).getData();
        // 判断是否收藏过再点击
        if (collectList != null && collectList.size() > 0) {
            for (ItemsCollect collect : collectList) {
                if (collect.getSkuid().equals(itemsCollect.getSkuid())) {
                    // 是的话就取消收藏
                    itemsCollectFeign.delete(collect.getId());
                    return new Result(false, StatusCode.ERROR,"取消收藏成功");
                }
            }
        }
        // 添加收藏商品数据到数据库
        itemsCollect.setId("No" + idWorker.nextId());
        itemsCollectFeign.add(itemsCollect);
        itemsCollect.setSku(skuFeign.findById(id).getData());
        // 返回信息
        return new Result(true, StatusCode.OK,"收藏成功");
    }
}
